//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package com.marakana.obolo;

import database.DBAdapter;
import game.Player;
import android.app.Dialog;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class StoryActivity extends GameActivity {
	private static Dialog storyDialog;
	private TextView txtDialeg;
	private String[] storyText;
	int count = 0;

	@Override
	protected void onStart() {
		super.onStart();

		Bundle extras = getIntent().getExtras();

		// Recupera els dialegs del personatge PORC
		storyText = extras.getStringArray("storytext");

		// Crea el storyDialog
		storyDialog = new Dialog(this);
		storyDialog.setOwnerActivity(this);
		storyDialog.setTitle("PORC");
		storyDialog.setCancelable(true);
		storyDialog.setContentView(R.layout.story_dialog);

		// Instancia els objectes del dialogStory
		txtDialeg = (TextView) storyDialog.findViewById(R.id.txtDialogStory);
		TextView txtContinue = (TextView) storyDialog
				.findViewById(R.id.txtContinueStory);
		txtContinue.setText(R.string.txtContinueStory);
		// Prepara la primera part de la conversa
		txtDialeg.setText(storyText[0]);
		count++;
		storyDialog.show();

		txtContinue.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Abansa en la conversa fins que acaba i es tanca el dialeg
				if (count < storyText.length) {
					txtDialeg.setText(storyText[count]);
					count++;
				} else
					storyDialog.dismiss();
			}
		});
	}

	@Override
	protected void finishGame(Player winner, Player looser) {
		// finishActivity(RESULT_OK);
		if (winner != null && winner.isHuman()) {
			DBAdapter dbPlayer = new DBAdapter(this);
			dbPlayer.open();
			int estat = dbPlayer.getPlayer(1).getInt(5);
			estat++;
			Cursor cPlayer = dbPlayer.getPlayer(1);
			dbPlayer.updatePlayer(cPlayer.getLong(0), cPlayer.getString(1),
					cPlayer.getString(2), cPlayer.getLong(3),
					cPlayer.getLong(4), estat, cPlayer.getLong(6));
			dbPlayer.close();
		}
		finish();
	}

}
